﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Noris.Tools.FrxEditor.Components;
using Noris.Schedule.Support;

namespace Noris.Tools.FrxEditor.Editor
{
    #region class InteractiveArea
    /// <summary>
    /// Interaktivní prostor.
    /// Jde o datovou třídu (nikoli Control), která implementuje interface IInteractiveObject,
    /// proto může být umístěna do objektu, který reprezentuje interaktivní obsah (EditorControl).
    /// </summary>
    public class InteractiveArea : IInteractiveObject
    {
        public InteractiveArea()
        {
            this.BackColor = Color.LightGray;
            this.LayerStandard = 0;
            this.LayerInteractive = 0;
            this.MouseState = EditMouseState.None;
        }
        public Color BackColor { get; set; }
        public int LayerStandard { get; set; }
        public int LayerInteractive { get; set; }
        public EditMouseState MouseState { get; private set; }

        public event EventHandler Click;

        public virtual Image ImageCurrent { get { return null; } }
        /// <summary>
        /// Řízení interaktivity - odpověď na akci myši
        /// </summary>
        /// <param name="args"></param>
        public virtual void MouseChange(EditInteractiveEventArgs args)
        {
            switch (args.MouseChange)
            {
                case EditMouseChangeType.Enter:
                    this.MouseState = EditMouseState.OnControl;
                    args.ReDrawLayers(this.LayerStandard, this.LayerInteractive);
                    break;
                case EditMouseChangeType.LeftDown:
                    this.MouseState = EditMouseState.MouseLeftDown;
                    args.ReDrawLayers(this.LayerInteractive);
                    break;
                case EditMouseChangeType.LeftUp:
                    this.OnClick();
                    this.MouseState = EditMouseState.OnControl;
                    args.ReDrawLayers(this.LayerInteractive);
                    break;
                case EditMouseChangeType.Leave:
                    this.MouseState = EditMouseState.Outside;
                    args.ReDrawLayers(this.LayerInteractive, this.LayerStandard);
                    break;
            }
        }
        /// <summary>
        /// Řízení vykreslování
        /// </summary>
        /// <param name="args"></param>
        public virtual void Paint(EditPaintEventArgs args)
        {
            UiRenderer.DrawBackground(args.Graphics, args.VisualArea, this.BackColor, 45, RenderStyle.FlatStyle, args.MouseState);
            Image image = this.ImageCurrent;
            if (image != null)
            {
                Size visualSize = Size.Truncate(args.VisualArea.Size.Reduce(1f));
                SizeF imageSize = image.Size.ShrinkTo(visualSize, true);
                RectangleF imageArea = UiRenderer.AlignSizeIntoArea(args.VisualArea, imageSize, System.Drawing.ContentAlignment.MiddleCenter);
                args.Graphics.DrawImage(image, imageArea);
            }
        }
        /// <summary>
        /// Událost po kliknutí, vyvolá event Click
        /// </summary>
        protected virtual void OnClick()
        {
            if (this.Click != null)
                this.Click(this, EventArgs.Empty);
        }
    }
    #endregion
    #region class InteractiveButton
    /// <summary>
    /// Obecný objekt, který může být vykreslován a může mít interaktivní chování
    /// </summary>
    public class InteractiveButton : InteractiveArea
    {
        public InteractiveButton()
        {
            this.Enabled = true;
        }
        public bool Enabled { get; set; }
        public Image Image { get; set; }
        public Image ImageHot { get; set; }
        public Image ImagePressed { get; set; }
        public Image ImageDisable { get; set; }
        public override Image ImageCurrent
        {
            get
            {
                Image result = null;
                if (!this.Enabled)
                    return Steward.FirstNotNull(this.ImageDisable, this.Image);

                switch (this.MouseState)
                {
                    case EditMouseState.MouseLeftDown:
                        return Steward.FirstNotNull(this.ImagePressed, this.ImageHot, this.Image);
                    case EditMouseState.OnControl:
                        return Steward.FirstNotNull(this.ImageHot, this.Image);
                    default:
                        return this.Image;
                }
            }
        }
    }
    #endregion
}
